import 'package:xws/helpers/as_t.dart';
import 'package:xws/res/images.dart';

import 'comment.dart';
import 'reply_obj.dart';

/// 评论对象
class CommentObj extends Comments {
  CommentObj({
    final String? id,
    final String? goodsId,
    final String? userId,
    required String nickName,
    final String? avatarUrl,
    required String createdAt,
    final String? content,
    required this.replyList,
  }) : super(
          id: id,
          goodsId: goodsId,
          userId: userId,
          nickName: nickName,
          avatarUrl: avatarUrl,
          createdAt: createdAt,
          content: content,
        );

  factory CommentObj.fromJson(Map<String, dynamic> jsonRes) {
    final List<ReplyObj> _replyList = <ReplyObj>[];
    final List<dynamic> cd = jsonRes['replyList'] as List<dynamic>? ?? <dynamic>[];
    if (cd.isNotEmpty) {
      for (final dynamic item in cd) {
        if (item != null) {
          _replyList.add(ReplyObj.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    String _avatarUrl = asT<String>(jsonRes['avatarUrl']);
    if (_avatarUrl.isEmpty) _avatarUrl = R.defHeader;

    return CommentObj(
      id: asT<String?>(jsonRes['id']),
      goodsId: asT<String?>(jsonRes['goodsId']),
      userId: asT<String?>(jsonRes['userId']),
      nickName: asT<String>(jsonRes['nickName']),
      avatarUrl: _avatarUrl,
      createdAt: asT<String>(jsonRes['createdAt']),
      content: asT<String?>(jsonRes['content']),
      replyList: _replyList,
    );
  }

  late List<ReplyObj> replyList;
}
